<?php

namespace MyBlog\Http\Controllers\backend;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use MyBlog\Http\Controllers\Controller;
use MyBlog\Models\ManageLog;
use MyBlog\Models\Manager;

class LoginController extends BaseController
{
    public function login()
    {
        return View::make('backend.login');
    }

    public function attempt()
    {
        $validation = Validator::make(Input::all(), Manager::$rules, Manager::$msgs);
        if($validation->fails()) {
            return Redirect::back()
                ->withInput()
                ->withErrors($validation);
        }
        if (Auth::attempt([
            'name' => Input::get('name'),
            'password' => Input::get('password'),
        ])) {
            ManageLog::create([
                'manager_name' => Input::get('name'),
                'ip' => $_SERVER["REMOTE_ADDR"],
                'action' => '管理员登录',
            ]);
            return redirect()->intended('backend/index');
        }
        Session::flash('msg', '您输入的帐号或密码有误。');
        return Redirect::back();
    }

    public function logout()
    {
        Auth::logout();
        return Redirect::to('backend/login');
    }




}
